﻿namespace VectorPicsEditor.Controls
{
    using System;
    using System.ComponentModel;
    using System.Windows.Controls;
    using System.Windows.Forms;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using VectorPicsEditor.Helpers;

    public partial class PreviewProperties : CommonProperties
    {
        public PreviewProperties()
        {
            InitializeComponent();
        }

        public override void Initialize(ShapeWrapper shape, System.Windows.Forms.ComboBox comboBox)
        {
            base.Initialize(shape, comboBox);
            this.InitFields();
        }

        private void InitFields()
        {
            this.thicknessTextBox.Text = (base.Shape.Shape as Rectangle).StrokeThickness.ToString();
            this.colorPanel.BackColor = Converter.ConvertColor(((this.Shape.Shape as Rectangle).Stroke as SolidColorBrush).Color);
            this.opacityTrackBar.Value = (int)(this.Shape.Shape.Opacity * 100);
            this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
        }

        private void ChangeColor(object sender, EventArgs e)
        {
            var newColor = base.OpenColorDialog();
            if (newColor != null)
            {
                this.colorPanel.BackgroundImage = null;
                this.colorPanel.BackColor = newColor.Value;
                (this.Shape.Shape as Rectangle).Stroke = new SolidColorBrush(Converter.ConvertColor(newColor.Value));
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeThickness(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.thicknessTextBox.Text);
                (this.Shape.Shape as Rectangle).StrokeThickness = x;
            }
            catch (FormatException)
            {
                //Ignore this
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeOpacity(object sender, EventArgs e)
        {
            this.Shape.Shape.Opacity = this.opacityTrackBar.Value / 100f;
            this.Shape.WasChanged = true;
        }

        private void ChangeZindex(object sender, EventArgs e)
        {
            try
            {
                int c = int.Parse(this.zindexTextBox.Text);
                Canvas.SetZIndex(this.Shape.Shape, c);
            }
            catch (FormatException)
            {
                this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

    }
}
